﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;

namespace ThisIsTheOnlyChamber
{
    class Camera
    {
        private float theta, phi, fov;
        public float AspectRatio;

        public Vector3 position;

        public Camera(Vector3 position, float fov = MathHelper.Pi / 3.5f, float aspectRatio = MathHelper.PiOver4, float theta = MathHelper.PiOver2, float phi = MathHelper.Pi / 9f )
        {
            this.AspectRatio = aspectRatio;
            this.fov = fov;
            this.position = position;
            this.theta = theta;
            this.phi = phi;
        }

        public Matrix View
        {
            get
            {
                return Matrix.CreateTranslation(-position) *
                       Matrix.CreateRotationY(theta + MathHelper.PiOver2) *
                       Matrix.CreateRotationX(phi);
            }
        }

        public Matrix Projection
        {
            get
            {
                return Matrix.CreatePerspectiveFieldOfView(fov, AspectRatio, 9f, 12000f);
            }
        }
        
        public void Strafe (float delta)
        {
            position.Z += delta * (float)Math.Sin(theta + MathHelper.PiOver2);
            position.X += delta * (float)Math.Cos(theta + MathHelper.PiOver2);
        }

        public void MoveForward(float delta)
        {
            position.Z += delta * (float)Math.Sin(theta);
            position.X += delta * (float)Math.Cos(theta);
        }

        public void MoveUp(float delta)
        {
            position.Y += delta;
        }

        public void ChangeTheta(float delta)
        {
            theta += delta;
        }

        public void ChangePhi(float delta)
        {
            phi -= delta;
            if (phi > MathHelper.PiOver2)
                phi = MathHelper.PiOver2;
            if (phi < -MathHelper.PiOver2)
                phi = -MathHelper.PiOver2;
        }
    }
}
